<?php
namespace jm\helper;


class Request
{
    static public function get($url, $header=[], $request=''){
        return self::httpCurl($url, $header, 'get', $request);
    }

    static public function post($url, $header=[], $request=''){
        return self::httpCurl($url, $header, 'post', $request);
    }

    /**
     * @param  $url     string
     * @param  $header  array
     * @param  $type    string
     * @param  $request array
     */
    private static function httpCurl($url, $header=[], $type='get', $request=''){
        // 1、初始化curl
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        
        // 2、设置参数
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        
        // post方式
        if($type == 'post'){
            curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
            curl_setopt($ch, CURLOPT_POSTFIELDS, $request);
        }
        
        // put方式
        if($type == 'put'){
            curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");// 定义请求类型
            curl_setopt($ch, CURLOPT_POSTFIELDS, $request);// 定义提交的数据
        }
        
        // delete方式
        if($type == "delete")
        {
            curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE");// 定义请求类型
            curl_setopt($ch, CURLOPT_POSTFIELDS, $request);// 定义提交的数据
        }
        
        if($type == "patch")
        {
            curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PATCH");// 定义请求类型
            curl_setopt($ch, CURLOPT_POSTFIELDS, $request);// 定义提交的数据
        }
        curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
        
        // 3、调用接口
        $res = curl_exec($ch);
        $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);// 返回http状态码

        if(curl_errno($ch)){
            // 请求失败，返回错误信息
            $error = curl_error($ch);
            $curl_error = json(['error'=>$error]);
            return $curl_error ;

        }
        
        // 请求成功，返回json；
        // 4、关闭curl
        curl_close($ch);
        return $res;
    }

    // 发送短信
    public static function sms($url, $params=false, $ispost=false)
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
        curl_setopt($ch, CURLOPT_TIMEOUT, 30);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        
        if ($ispost) {
            curl_setopt($ch, CURLOPT_POST, true);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
            curl_setopt($ch, CURLOPT_URL, $url);
        } else {
            if ($params) {
                curl_setopt($ch, CURLOPT_URL, $url . '?' . $params);
            } else {
                curl_setopt($ch, CURLOPT_URL, $url);
            }
        }
        
        $response = curl_exec($ch);
        if ($response === false) {
            return false;
        }

        curl_close($ch);
        return $response;
    }

    // smschinese 发送短信（官方提供示例代码）
    public static function smschinese($url)
    {
        $ch = curl_init();
        // curl_init()需要php_curl.dll扩展
        $timeout = 5;
        curl_setopt ($ch, CURLOPT_URL, $url);
        curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
        $file_contents = curl_exec($ch);
        curl_close($ch);
        return $file_contents;
    }

    // 极光推送
    // $request 需要是一个json字符串；
    public static function jpush($url, $auth, $request) {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type:application/json']);
        // curl_setopt($ch, CURLOPT_USERPWD, $auth);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization: Basic '. base64_encode($auth)));
        curl_setopt($ch, CURLOPT_POSTFIELDS, $request);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        $response = curl_exec($ch);
        $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        $errno = curl_errno($ch);
        $error = curl_error($ch);
        curl_close($ch);
        return [
            'response' => $response,
            'httpCode' => $httpCode,
            'errno' => $errno,
            'error' => $error,
        ];
    }


    // 代理webdav的图片文件（失败了）
    public static function agentWebdav() {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, "http://58.57.117.132:9801/remote.php/webdav/yyyyyyyy/1.png");// 没有 Content-Dispostion:attachment;
        // curl_setopt($ch, CURLOPT_URL, "http://58.57.117.183:8000/remote.php/webdav/12345678901/capture/1.png");// 有 Content-Dispostion:attachment;
        // curl_setopt($ch, CURLOPT_PORT, 443);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_TIMEOUT, 15);
        curl_setopt($ch, CURLOPT_HEADER, 1);
        curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
        // 下面这两行，用那个都可以的；
        // curl_setopt($ch, CURLOPT_USERPWD, "15800715040:123456");
        // curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization: Basic '. base64_encode("15800715040:123456")));
        
        // 下面这两行就不行了，即使同时用也不行；
        // curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization: auth'));
        // curl_setopt($ch, CURLOPT_HTTPHEADER, array('auth: '. base64_encode("15800715040:123456")));
        
        curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
        curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);
          
        $return = curl_exec($ch);
        if (!$return) {
            echo curl_error($ch);
        }
        else {
            // 获得响应结果里的：头大小
            $headerSize = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
            // 根据头大小去获取头信息内容
            // $header = substr($return, 0, $headerSize);
            // echo $header;

            header('Content-Type:image/png');
            // header('Content-type: image/JPEG');
            // echo $return;
            echo substr($return, $headerSize);
            // echo '<img src="data:image/png;base64,'. base64_encode(substr($return, $headerSize)) .'">' ;
            // \Dev::ve(json_decode($return, true));
        }

        curl_close($ch);
    }
}